﻿
using Ku.AppCore;
using Ku.AppCore.Regions;
using Ku.Common;
using Ku.Start.Views;
using Prism.Dialogs;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Navigation.Regions;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;

namespace Ku.Start
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        public App()
        {
            // 设置全局异常处理程序
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
            Application.Current.Exit += ExitEventHandler;
        }
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="containerRegistry"></param>
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //containerRegistry.RegisterSingleton<IMessageService, MessageService>();

            //注册控件
            containerRegistry.RegisterForNavigation<Views.Home>("Home");

            //containerRegistry.RegisterForNavigation<UC_LayoutContent>("UC_LayoutContent");
            //containerRegistry.RegisterForNavigation<UC_LayoutMenu>("UC_LayoutMenu");

            //containerRegistry.RegisterForNavigation<UC_Dialog_A>("UC_Dialog_A");

            //注册对话 
            //containerRegistry.RegisterDialog<DemoDialog, DemoDialogViewModel>();
            //containerRegistry.RegisterDialog<LoginView, LoginViewModel>("LoginView");


        }

        protected override void OnInitialized()
        {
            var service = App.Current.MainWindow.DataContext as IConfigureService;
            if (service != null)
                service.Configure();
            base.OnInitialized();
        }

        /// <summary>
        /// Directory配置模块目录: 通过读取根目录Modules文件夹查找模块
        /// </summary>
        /// <returns></returns>
        protected override IModuleCatalog CreateModuleCatalog()
        {
            var modulePath = @".\Modules";
            if (!Directory.Exists(modulePath)) Directory.CreateDirectory(modulePath);
            string[] files = Directory.GetFiles(modulePath);
            return new DirectoryModuleCatalog { ModulePath = modulePath };
        }
        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            //moduleCatalog.AddModule<ModuleNameModule>();
        }

        protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
        {
            base.ConfigureRegionAdapterMappings(regionAdapterMappings);
            //regionAdapterMappings.RegisterMapping(typeof(HandyControl.Controls.TabControl), Container.Resolve<TabControlRegionAdapter>());
        }

        #region 异常事件处理
        protected override void OnExit(ExitEventArgs e)
        {
            Application.Current.Shutdown();
            base.OnExit(e);
        }
        private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            // 处理线程异常
            HandleException(e.Exception);
            e.Handled = true; // 表示异常已处理 
            // 在这里执行自定义的异常处理逻辑，例如显示错误消息 
            Debug.WriteLine($"发生错误: {e.Exception.Message}");
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            // 处理全局异常
            Exception exception = e.ExceptionObject as Exception;
            HandleException(exception);
            Debug.WriteLine($"发生错误: {exception.Message}");
        }
        private void HandleException(Exception exception)
        {
            // 在这里执行自定义的异常处理逻辑
            // 例如，记录异常、显示错误消息、关闭应用程序等
            // 请根据您的需求自定义处理方法
            Debug.WriteLine($"发生错误: {exception.Message}");
            LogsRecord.write("Exception", exception.ToString());
        }
        private void ExitEventHandler(object sender, ExitEventArgs e)
        {
            System.Environment.Exit(0);
        }
        #endregion
    }
}
